import { _decorator, Button, Component, Tween, tween, v3, Vec3, Widget } from 'cc';
import { DataProxy } from '../../../frame/A-LIB/lib.b.data';
import { AudioManager } from '../../../frame/component/audio-manager';
import GameDC from '../../manager/data.game.center';
import GameManager from '../../manager/game.manager';
import { Popup } from './popup';
const { ccclass, property } = _decorator;

@ccclass('TreasureBox')
export class TreasureBox extends Component {
	@property(Button)
	private button: Button = null;

	private va: mtec.cc.VecAssist;
	private path: Vec3[];
	private calculate_vec: Vec3 = v3();
	private flying = false;
	private curr_index = 0;
	private interval = 60;
	private wgt: Widget;
	private speed = 100 / 0.5;

	private status: 'pause'|'flying';

	onLoad() {
		this.init();
	}

	start(){
		this.node.active = false;

		if(GameDC.User.round) void 0;
		else{
			this.hide();
			this.node.active = true;
		}
	}

	private init(){
		this.va = new mtec.cc.VecAssist(this.button.node);
		this.path = mtec.array.create(5, ()=>v3());
		this.wgt = this.node.getComponent(Widget);

		DataProxy.follow(GameDC.Trigger, 'UILuckyBox', (_, show)=>{
			if(show != _){
				if(show) this.show();
				else{
					this.pause();
					this.hide();
				}
			}
		}, false);
	}

	private onClick(){
		AudioManager.playEffect(AudioManager.EAudio.EF_CLICK);
		if(Popup.lock){
			return void 0;
		}
		Popup.lock = true;
		this.pause();
		GameManager.userPoint('ClickLuckyBox');
		Popup.Treasurebox().then(reap=>{
			if(reap){
				GameManager.userPoint('GetLuckyBox');
				GameManager.changeDiamond(3);
				GameManager.reapDiamond(3, mtec.cc.canvas.node.position);
				this.hide();
				this.scheduleOnce(()=>this.show(), this.interval);
			}else this.resume();
			Popup.lock = false;
		});
	}

	private refreshWidget(){
		[this.wgt.top, this.wgt.bottom, this.wgt.left, this.wgt.right] = [mtec.cc.is_long_screen ? 590 : 490, 570, 0, 0];
		this.wgt.enabled = true;
		this.wgt.updateAlignment();
		this.wgt.enabled = false;
	}

	private show(){
		if(!this.node.active) this.node.active = true;
		this.button.node.active = true;
		Tween.stopAllByTarget(this.button.node);
		this.refreshWidget();

		if(this.flying) this.resume();
		else{
			this.flying = true;
			let {top, bottom, right, left} = this.va;
			this.path.forEach((vec, i)=>{
				vec.x = (i%2)==0 ? left : right;
				vec.y = mtec.number.random(top, bottom, 2);
				vec.z = this.button.node.position.z;
			});

			this.path[0].x = this.va.over_left;
			this.path.lastElement.x = this.va.over_left;
			this.curr_index = 0;
			this.button.node.setPosition(this.path[0]);
			this.next();
			this.status = 'flying';
		}
	}

	private hide(){
		this.unscheduleAllCallbacks();
		this.flying = false;
		this.button.node.active = false;
	}

	private pause(){
		this.status = 'pause';
		Tween.stopAllByTarget(this.button.node);
	}

	private resume(){
		if(this.status!='pause') return void 0;
		this.status = 'flying';
		this.curr_index--;
		this.next();
	}

	private next(){
		this.curr_index++;
		Tween.stopAllByTarget(this.button.node);

		if(this.curr_index>=this.path.length){
			this.hide();
			this.scheduleOnce(()=>this.show(), this.interval);
		}else{
			let time = Vec3.subtract(this.calculate_vec, this.button.node.position, this.path[this.curr_index]).length() / this.speed;
			tween(this.button.node)
			.to(time, {position: this.path[this.curr_index]})
			.call(()=>this.next()).start();
		}
	}
}

